<?php
/**
 * @file: Attachmentsm.php
 * @User: shiwuhao
 * @Date: 16/5/5 16:07
 * @synopsis:
 */

class Attachmentsm extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取帖子的图片信息
     * @param $post_id
     * @param int $cache  是否从缓存中取
     * @return array|mixed
     */
    public function get_post_img_info($post_id, $cache = 1)
    {
        $key      = POST_IMG_INFO_PREFIX.$post_id;
        $img_info = unserialize($this->redis->get($key));
        if (empty($img_info['img_list']) || $cache == 0) {
            $filter  = array('pid' => $post_id, 'cid' => 0);
            $results = $this->db->select('original,small,width,height')->where($filter)->order_by('sort','ASC')->get('ml_attachments')->result_array();

            $img_list = $share_img_list = $img_ratio = array();
            foreach($results as $v) {
                $img_list[]       = ret_fullpath_url($v['original']); // 图片列表
                $share_img_list[] = ret_fullpath_url($v['small']);    // 分享图片列表
                $img_ratio[]      = $v['width'] ? $v['height'] / $v['width'] : 1.16; // 图片比例
            }

            $img_info = array('img_list'=> $img_list, 'share_img_list' => $share_img_list, 'img_ratio' => $img_ratio);
            $this->redis->save($key, serialize($img_info), POST_IMG_INFO_KEY_EXPIRE_TIME);
        }

        return $img_info;
    }
}